import { BaseModel } from '@/common/base/base.model';
import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany, OneToOne } from 'typeorm';
import { BrandModel } from "@/module/brand/brand.model";
import { ArticleModel } from "@/module/article/article.model";
import { EvaluationDataModel } from "@/module/evaluation-data/evaluation-data.model";
import { moneyTransformer } from "@/common/utils/utils";
import { EvaluationSpecificationModel } from "@/module/evaluation-specification/evaluation-specification.model";
import { EvaluationCategory, EvaluationStatus } from "common/utils/data";
import { EvaluationFileModel } from "@/module/evaluation-file/evaluation-file.model";

@Entity('EVALUATION', {
  synchronize: false,
}) // 产品评测
export class EvaluationModel extends BaseModel {
  @Column({
    comment: '标题',
    type: 'varchar',
    length: 50,
  })
  title: string;

  @Column({
    comment: '分类',
    type: 'varchar',
    length: 50,
  })
  category: EvaluationCategory;

  @Column({
    comment:
      '状态（UNPUBLISH：未发布, PUBLISH: 已发布）',
    type: 'varchar',
    length: 50,
    default: EvaluationStatus.UNPUBLISH,
  })
  status: EvaluationStatus;

  @Column({
    comment: '品牌uuid',
    type: 'uuid',
  })
  @Index('EVALUATION.brand_uuid')
  brandUuid: string;
  @ManyToOne(() => BrandModel, {
    createForeignKeyConstraints: false
  })
  @JoinColumn({
    name: 'brand_uuid',
    referencedColumnName: 'uuid',
  })
  brand: BrandModel;

  @Column({
    comment: '文章uuid',
    type: 'uuid',
  })
  @Index('EVALUATION.article_uuid')
  articleUuid: string;
  @OneToOne(() => ArticleModel, {
    createForeignKeyConstraints: false
  })
  @JoinColumn({
    name: 'article_uuid',
    referencedColumnName: 'uuid',
  })
  article: ArticleModel;

  @OneToMany(() => EvaluationDataModel, (evaluationData) => evaluationData.evaluation)
  datas: EvaluationDataModel[];

  @OneToMany(() => EvaluationSpecificationModel, (evaluationSpecification) => evaluationSpecification.evaluation)
  specifications: EvaluationSpecificationModel[];

  @OneToMany(() => EvaluationFileModel, (evaluationFile) => evaluationFile.evaluation)
  impulseResponseFiles: EvaluationFileModel[];

  @Column({
    comment: '价格',
    type: 'decimal',
    transformer: moneyTransformer,
    nullable: true,
  })
  price?: number;

  @Column({
    comment: '购买链接',
    type: 'varchar',
    length: 500,
    default: ''
  })
  buyUrl: string;
}
